//
//  BaesVC.m
//  OpenEyes
//
//  Created by dllo on 15/9/9.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "BaesVC.h"
#import "PicClassVC.h"
#import "SaveViewControllerForVideo.h"
@interface BaesVC ()

@property(nonatomic, strong)UIView *downView;
@property(nonatomic, strong)UIView *SaveView;
@property(nonatomic, strong)UIView *topView;
@property(nonatomic, strong)UIAlertView *aView;
@property(nonatomic, strong)UIImageView *leftBackView;
@property(nonatomic, assign)BOOL isDetail;
@end

@implementation BaesVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    

    
    // Do any additional setup after loading the view.
    
}

- (void)creatTopView
{
    self.topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
    self.isDetail = NO;
    self.topView.backgroundColor = [UIColor whiteColor];
    [self.navigationController.view addSubview:self.topView];
    [self creatTopViewNext];
}
- (void)creatTopViewForDetail
{
    self.topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
    self.isDetail = YES;
    self.topView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.topView];
    [self creatTopViewNext];
}


- (void)creatTopViewNext{

    self.topTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 70, 30)];
    self.topTitle.center = CGPointMake(WIDTH / 2, 49 / 2 + 15);
    self.topTitle.textAlignment = NSTextAlignmentCenter;
    self.topTitle.text = @"Daily";
    self.topTitle.font = [UIFont fontWithName:@"AmericanTypewriter-Bold" size:20];
    [self.topView addSubview:self.topTitle];
    
    self.leftBuName = [[UILabel alloc] initWithFrame:CGRectMake(45, 19, 80, 40)];
    self.leftBuName.textAlignment = NSTextAlignmentLeft;
    self.leftBuName.text = @"Today";
    self.leftBuName.font = [UIFont fontWithName:@"AmericanTypewriter" size:13];
    [self.topView addSubview:self.leftBuName];
    
    self.leftBackView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 32, 15, 15)];
    self.leftBackView.image = [UIImage imageNamed:@"liebiao"];
    [self.topView addSubview:self.leftBackView];
    
    self.leftBu = [UIButton buttonWithType:UIButtonTypeCustom];
    self.leftBu.frame = CGRectMake(0, 15, 65, 45);
    self.leftBu.backgroundColor = [UIColor clearColor];
    [self.leftBu addTarget:self action:@selector(leftBuAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.topView addSubview:self.leftBu];
    
    self.rightBackView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH - 40, 29, 20, 20)];
    self.rightBackView.image = [UIImage imageNamed:@"chakan2"];
    [self.topView addSubview:self.rightBackView];
    
    self.rightBu = [UIButton buttonWithType:UIButtonTypeCustom];
    self.rightBu.backgroundColor = [UIColor clearColor];
    self.rightBu.frame = CGRectMake(WIDTH - 70, 15, 65, 45);
    //    [self.rightBu setBackgroundImage:[UIImage imageNamed:@"chakan2"] forState:UIControlStateNormal];
    [self.rightBu addTarget:self action:@selector(rightBuAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.topView addSubview:self.rightBu];
}
#pragma mark -
#pragma mark 顶端左侧按钮
- (void)leftBuAction:(UIButton *)button
{
    [self createShoucangList];
    [self addGesture];
    [button removeTarget:self action:@selector(leftBuAction:) forControlEvents:UIControlEventTouchUpInside];
    [button addTarget:self action:@selector(leftBuAction2:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)leftBuAction2:(UIButton *)button
{
    [self removeShoucangList];
    [button removeTarget:self action:@selector(leftBuAction2:) forControlEvents:UIControlEventTouchUpInside];
    [button addTarget:self action:@selector(leftBuAction:) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark -
#pragma mark 创建收藏列表
- (void)createShoucangList
{
    self.SaveView = [[UIView alloc] initWithFrame:CGRectMake(0, 128 - HEIGHT, WIDTH, HEIGHT - 64)];
    self.SaveView.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:1 alpha:1];
    [self.view addSubview:self.SaveView];
    [self.view bringSubviewToFront:self.topView];
    [UIView animateWithDuration:0.2 animations:^{
        
        
        if (self.isDetail) {
            self.SaveView.frame = CGRectMake(0, 64, WIDTH, HEIGHT - 64);
        }else {
            self.SaveView.frame = CGRectMake(0, 0, WIDTH, HEIGHT - 64);
        }
        
        
        [self.leftBuName setHidden:YES];
        [self.rightBu setHidden:YES];
        self.leftBackView.transform = CGAffineTransformRotate(self.leftBackView.transform, M_PI_2);
    }];
    
    UIButton *saveButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    saveButton.center = CGPointMake(WIDTH / 2, 70);
    [saveButton setTitle:@"我的收藏" forState:UIControlStateNormal];
    saveButton.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:15];
    [saveButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [saveButton addTarget:self action:@selector(clickSaveButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.SaveView addSubview:saveButton];
    
    UIButton *downLoadButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    downLoadButton.center = CGPointMake(WIDTH / 2, 140);
    [downLoadButton setTitle:@"我的下载" forState:UIControlStateNormal];
    downLoadButton.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:15];
    [downLoadButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [downLoadButton addTarget:self action:@selector(clickDownLoadButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.SaveView addSubview:downLoadButton];
    
    UIButton *cleanCacheButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 150, 30)];
    cleanCacheButton.center = CGPointMake(WIDTH / 2, 210);
    [cleanCacheButton setTitle:@"清除所有缓存" forState:UIControlStateNormal];
    cleanCacheButton.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:15];
    [cleanCacheButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [cleanCacheButton addTarget:self action:@selector(clickcleanCacheButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.SaveView addSubview:cleanCacheButton];
    
    UIButton *wifiButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 150, 30)];
    wifiButton.center = CGPointMake(WIDTH / 2, 280);
    [wifiButton setTitle:@"使用流量时提醒我" forState:UIControlStateNormal];
    wifiButton.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:15];
    [wifiButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [wifiButton addTarget:self action:@selector(clickWifiButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.SaveView addSubview:wifiButton];
    
}
//收藏
- (void)clickSaveButton:(UIButton *)button
{
    SaveViewControllerForVideo *saveVC = [[SaveViewControllerForVideo alloc] init];
    saveVC.modalTransitionStyle = 2;
    [self presentViewController:saveVC animated:YES completion:^{
        
    }];
}
//下载
//- (void)clickDownLoadButton:(UIButton *)button
//{
//    DownLoadController *downVC = [[DownLoadController alloc] init];
//    downVC.modalTransitionStyle = 2;
//    [self presentViewController:downVC animated:YES completion:^{
//        
//    }];
//}
#pragma mark -
#pragma mark 清除缓存
- (void)clickcleanCacheButton:(UIButton *)button
{
    NSFileManager *manager = [NSFileManager defaultManager];
    NSArray *cachesArr = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachesPath = cachesArr[0];
    CGFloat a = [[manager attributesOfItemAtPath:cachesPath error:nil] fileSize];
    NSLog(@"%g", a);
    
    BOOL result = [manager removeItemAtPath:cachesPath error:nil];
    if (result) {
        self.aView = [[UIAlertView alloc] initWithTitle:@"删除成功" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [self.aView show];
    }
}
- (void)clickWifiButton:(UIButton *)button
{
    
}

#pragma mark -
#pragma mark 顶端右侧按钮
- (void)rightBuAction:(UIButton *)button
{
    
}








- (void)removeShoucangList
{
    [UIView animateWithDuration:0.2 animations:^{
        self.SaveView.frame = CGRectMake(0, 128 - HEIGHT, WIDTH, HEIGHT - 64);
        self.leftBackView.transform = CGAffineTransformRotate(self.leftBackView.transform, -M_PI_2);
    } completion:^(BOOL finished) {
        [self.leftBuName setHidden:NO];
        self.rightBu.hidden = NO;
        [self.SaveView removeFromSuperview];
    }];
}
#pragma mark -
#pragma mark createDownView
- (void)createDownView
{
    self.downView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT - 40, WIDTH, 40)];
    self.downView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.downView];
    
    UIButton *leftbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftbutton.frame = CGRectMake(0, 0, WIDTH / 2, 40);
    [leftbutton setTitle:@"每日精选" forState:UIControlStateNormal];
    [leftbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    leftbutton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];
    [leftbutton addTarget:self action:@selector(myleftbutton:) forControlEvents:UIControlEventTouchUpInside];
    leftbutton.layer.borderWidth = 0.3;
    leftbutton.layer.borderColor = [UIColor colorWithWhite:0.9 alpha:1].CGColor;
    [self.downView addSubview:leftbutton];
    
    UIButton *rightbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightbutton.frame = CGRectMake(WIDTH / 2, 0, WIDTH / 2, 40);
    [rightbutton setTitle:@"往期分类" forState:UIControlStateNormal];
    [rightbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    rightbutton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];
    [rightbutton addTarget:self action:@selector(myrightbutton:) forControlEvents:UIControlEventTouchUpInside];
    rightbutton.layer.borderWidth = 0.3;
    rightbutton.layer.borderColor = [UIColor colorWithWhite:0.9 alpha:1].CGColor;
    [self.downView addSubview:rightbutton];
}

- (void)myrightbutton:(UIButton *)button
{
    [self.tabBarController setSelectedIndex:1];
}
- (void)myleftbutton:(UIButton *)button
{
    [self.tabBarController setSelectedIndex:0];
}

#pragma mark -
#pragma mark 添加手势
- (void)addGesture
{
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(gestureAction)];
    [self.SaveView addGestureRecognizer:swipe];
    swipe.direction = UISwipeGestureRecognizerDirectionUp;
}
- (void)gestureAction
{
    [self leftBuAction2:self.leftBu];
}


- (void)addFileToCacheWithArr:(NSMutableArray *)arr
                          key:(NSString *)key
{
    NSFileManager *manager = [NSFileManager defaultManager];
    NSString *cachefile = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, 1, YES) firstObject];
    NSString *dirPath = [cachefile stringByAppendingPathComponent:@"daily"];
    if (![manager fileExistsAtPath:dirPath]) {
        [manager createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    NSString *fileName = [NSString stringWithFormat:@"%@.plist", key];
    NSString *filePath = [dirPath stringByAppendingPathComponent:fileName];
    
    [NSKeyedArchiver archiveRootObject:arr toFile:filePath];
    
}
- (NSArray *)getArrFromCacheWithKey:(NSString *)key
{
    NSString *cacheFile = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, 1, YES) firstObject];
    NSString *dailyFile = [cacheFile stringByAppendingPathComponent:@"daily"];
    NSString *str = [NSString stringWithFormat:@"%@.plist", key];
    NSString *dailyPlist = [dailyFile stringByAppendingPathComponent:str];
    return [NSKeyedUnarchiver unarchiveObjectWithFile:dailyPlist];
}












































- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
